// 引入
let express = require('express');
let fs = require('fs')
// 创建
let app = express();

app.get('/register', (reg, res) => {
    let al = reg.query;
    //读取person.json
    let result = fs.readFileSync('./persons.json', 'utf-8')
    result = JSON.parse(result)
    let index = result.findIndex(item => item.username === al.username && item.password===al.password);
    //根据索引去判断
    if (index === -1) {
        //新用户 追加
        result.push(al)
        res.send('注册成功');
    } else {
        res.send('用户名已被注册');
    }
    //当注册成功，追加到文件中
    result = JSON.stringify(result)
    fs.writeFileSync('./persons.json', result)
})
// 监听
app.listen(3000, () => {
    console.log('已经启动了');
})
